﻿using Joker.Model;
using Joker.View.Editor;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Joker.View.Management.TypeManagement {
    public partial class TypeManagement : Form {
        public TypeManagement() {
            InitializeComponent();
        }

        private void WarehouseOutTypeManagement_Load(object sender, EventArgs e) {
            comboBox1.SelectedIndex = 0;
            dataGridView1.DataSource = DBHelper.GetDataTable("SELECT * FROM [WarehouseInType]");
        }

        private void button1_Click(object sender, EventArgs e) {
            if (comboBox1.SelectedItem.Equals("入库")) {
                dataGridView1.DataSource = DBHelper.GetDataTable("SELECT * FROM [WarehouseInType]");
            } else {
                dataGridView1.DataSource = DBHelper.GetDataTable("SELECT * FROM [WarehouseOutType]");
            }
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e) {
            TypeDataEdit form = new TypeDataEdit();
            form.Method = comboBox1.SelectedItem.ToString();
            OpenForm.NewForm(this, form, false);
            if (comboBox1.SelectedItem.Equals("入库")) {
                dataGridView1.DataSource = DBHelper.GetDataTable("SELECT * FROM [WarehouseInType]");
            } else {
                dataGridView1.DataSource = DBHelper.GetDataTable("SELECT * FROM [WarehouseOutType]");
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) {
            TypeDataEdit form = new TypeDataEdit();
            form.Method = comboBox1.SelectedItem.ToString();
            form.TypeID = (int)dataGridView1.CurrentRow.Cells[0].Value;
            OpenForm.NewForm(this, form, false);
            if (comboBox1.SelectedItem.Equals("入库")) {
                dataGridView1.DataSource = DBHelper.GetDataTable("SELECT * FROM [WarehouseInType]");
            } else {
                dataGridView1.DataSource = DBHelper.GetDataTable("SELECT * FROM [WarehouseOutType]");
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) {
            int ID = (int)dataGridView1.CurrentRow.Cells[0].Value;
            if (MessageBox.Show($"你确定要删掉编号为{ID}的数据吗?") == DialogResult.Yes) {
                bool result = true;
                if (comboBox1.SelectedItem.Equals("入库")) {
                    result &= DBHelper.ExecuteNonQuery($"DELETE [WarehouseInType] WHERE ID = {ID}");
                } else {
                    result &= DBHelper.ExecuteNonQuery($"DELETE [WarehouseOutType] WHERE ID = {ID}");
                }
                if (result) {
                    MessageBox.Show("删除成功!");
                } else {
                    MessageBox.Show("删除失败!");
                }
            }
            if (comboBox1.SelectedItem.Equals("入库")) {
                dataGridView1.DataSource = DBHelper.GetDataTable("SELECT * FROM [WarehouseInType]");
            } else {
                dataGridView1.DataSource = DBHelper.GetDataTable("SELECT * FROM [WarehouseOutType]");
            }
        }
    }
}
